package tech.senri.iot.socket.model

import com.fasterxml.jackson.annotation.JsonIgnore
import io.swagger.annotations.ApiModelProperty
import java.text.SimpleDateFormat
import java.util.*

abstract class BaseMessage
{
    @ApiModelProperty("消息码", required = true, position = 0)
    var messageCode: Int = 0

    @ApiModelProperty("设备唯一ID", required = true, position = 1)
    var deviceId: String? = null

    @ApiModelProperty("功能码", required = true, position = 2)
    var funCode: Int = 0

    @JsonIgnore
    @ApiModelProperty(hidden = true)
    var dateTimeData: ByteArray = ByteArray(6)


    @ApiModelProperty("时间戳 年月日时分秒", required = true, position = 99)
    var dateTime: Long? = null
        @ApiModelProperty("时间戳 年月日时分秒", required = true)
        get()
        {
            Date().time / 1000
            val array = dateTimeData.joinToString(",") { String.format("%02d", it) }
            val sdf = SimpleDateFormat("yy,MM,dd,HH,mm,ss")
            return sdf.parse(array).time / 1000
        }
        set(value)
        {
            field = value
            val sdf = SimpleDateFormat("yy,MM,dd,HH,mm,ss")
            val str = sdf.format(Date(value!!))
            val strs = str.split(",")
            dateTimeData = strs.foldIndexed(ByteArray(strs.size)) { i, a, v -> a.apply { set(i, v.toByte()) } }
        }
}
